<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['productreceptline:productreceptline:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productreceptlineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="行ID" align="center" prop="lineId" />
      <el-table-column label="入库记录ID" align="center" prop="recptId" />
      <el-table-column label="库存记录ID" align="center" prop="materialStockId" /> -->
      <el-table-column label="产品物料ID" align="center" prop="itemId" />
      <el-table-column label="产品物料编码" align="center" prop="itemCode" />
      <el-table-column label="产品物料名称" align="center" prop="itemName" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="入库数量" align="center" prop="quantityRecived" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['productreceptline:productreceptline:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['productreceptline:productreceptline:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品入库记录行对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="productreceptlineRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="产品物料ID" prop="itemId">
          <el-input v-model="form.itemId" placeholder="请输入产品物料ID"  readonly />
        </el-form-item>
        <el-form-item label="产品物料编码" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入产品物料编码"  readonly/>
        </el-form-item>
        <el-form-item label="产品物料名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入产品物料名称"  readonly/>
        </el-form-item>
        <el-form-item label="规格型号" prop="specification">
          <el-input v-model="form.specification" type="textarea" placeholder="请输入内容"  readonly/>
        </el-form-item>
        <el-form-item label="单位" prop="unitOfMeasure">
          <el-input v-model="form.unitOfMeasure" placeholder="请输入单位"  readonly/>
        </el-form-item>
        <el-form-item label="入库数量" prop="quantityRecived">
          <el-input v-model="form.quantityRecived" placeholder="请输入入库数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Productreceptline">
import { listProductreceptline, getProductreceptline, delProductreceptline, addProductreceptline, updateProductreceptline } from "@/api/productreceptline/productreceptline"
import { number } from "echarts"
import { onMounted, watch } from "vue"

const { proxy } = getCurrentInstance()

const productreceptlineList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    recptId: null,
    materialStockId: null,
    itemId: null,
    itemCode: null,
    itemName: null,
    specification: null,
    unitOfMeasure: null,
    quantityRecived: null,
    workorderId: null,
    workorderCode: null,
    workorderName: null,
    batchCode: null,
    warehouseId: null,
    warehouseCode: null,
    warehouseName: null,
    locationId: null,
    locationCode: null,
    locationName: null,
    areaId: null,
    areaCode: null,
    areaName: null,
    expireDate: null,
    attr1: null,
    attr2: null,
    attr3: null,
    attr4: null,
  },
  rules: {
    itemId: [
      { required: true, message: "产品物料ID不能为空", trigger: "blur" }
    ],
    quantityRecived: [
      { required: true, message: "入库数量不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)
//定义接收父的值
const props = defineProps({
  currentRecptId:{
    type:[Number,String],
    required:true,
    default:null
  }
})
const isMounted = ref(false)
onMounted(()=>{
  isMounted.value=true
});
watch(()=>{
  ()=>props.currentRecptId;
  (newVal) => {
    console.log("父组件传递的工序ID：", newVal); // 切换工序时，日志应同步变化
    if (isMounted.value && newVal) { // 新增isMounted判断
      getList();
    }
  },
   { immediate: true }
})

/** 查询产品入库记录行列表 */
function getList() {
  loading.value = true
  const params ={
    ...queryParams.value,
    recptId:props.currentRecptId
  }
  listProductreceptline(params).then(response => {
    productreceptlineList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    lineId: null,
    recptId: props.currentRecptId,
    materialStockId: null,
    itemId: null,
    itemCode: null,
    itemName: null,
    specification: null,
    unitOfMeasure: null,
    quantityRecived: null,
    workorderId: null,
    workorderCode: null,
    workorderName: null,
    batchCode: null,
    warehouseId: null,
    warehouseCode: null,
    warehouseName: null,
    locationId: null,
    locationCode: null,
    locationName: null,
    areaId: null,
    areaCode: null,
    areaName: null,
    expireDate: null,
    remark: null,
    attr1: null,
    attr2: null,
    attr3: null,
    attr4: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("productreceptlineRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.lineId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加产品入库记录行"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _lineId = row.lineId || ids.value
  getProductreceptline(_lineId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改产品入库记录行"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["productreceptlineRef"].validate(valid => {
    if (valid) {
      if (form.value.lineId != null) {
        updateProductreceptline(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addProductreceptline(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _lineIds = row.lineId || ids.value
  proxy.$modal.confirm('是否确认删除产品入库记录行编号为"' + _lineIds + '"的数据项？').then(function() {
    return delProductreceptline(_lineIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('productreceptline/productreceptline/export', {
    ...queryParams.value
  }, `productreceptline_${new Date().getTime()}.xlsx`)
}

getList()
</script>
